package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("FoodbatchadditiveDAO")
@Transactional
public class FoodbatchadditiveDAOImpl extends AbstractJpaDao<Foodbatchadditive> implements FoodbatchadditiveDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Foodbatchadditive.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public FoodbatchadditiveDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Foodbatchadditive findFoodbatchadditiveById(Integer id) throws DataAccessException {

		return findFoodbatchadditiveById(id, -1, -1);
	}

	@Transactional
	public Foodbatchadditive findFoodbatchadditiveById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchadditiveById", startResult, maxRows, id);
			return (Foodbatchadditive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByName(String name) throws DataAccessException {

		return findFoodbatchadditiveByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchadditiveByName", startResult, maxRows, name);
		return new LinkedHashSet<Foodbatchadditive>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByNameContaining(String name) throws DataAccessException {

		return findFoodbatchadditiveByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchadditiveByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Foodbatchadditive>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByContent(String content) throws DataAccessException {

		return findFoodbatchadditiveByContent(content, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByContent(String content, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchadditiveByContent", startResult, maxRows, content);
		return new LinkedHashSet<Foodbatchadditive>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByContentContaining(String content) throws DataAccessException {

		return findFoodbatchadditiveByContentContaining(content, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByContentContaining(String content, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchadditiveByContentContaining", startResult, maxRows, content);
		return new LinkedHashSet<Foodbatchadditive>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByDescription(String description) throws DataAccessException {

		return findFoodbatchadditiveByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchadditiveByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Foodbatchadditive>(query.getResultList());
	}

	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByDescriptionContaining(String description)
			throws DataAccessException {

		return findFoodbatchadditiveByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFoodbatchadditiveByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Foodbatchadditive>(query.getResultList());
	}

	@Transactional
	public Foodbatchadditive findFoodbatchadditiveByPrimaryKey(Integer id) throws DataAccessException {
		return findFoodbatchadditiveByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Foodbatchadditive findFoodbatchadditiveByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchadditiveByPrimaryKey", startResult, maxRows, id);
			return (Foodbatchadditive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditivesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchadditivesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Foodbatchadditive> findAllFoodbatchadditives() throws DataAccessException {

		return findAllFoodbatchadditives(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchadditive> findAllFoodbatchadditives(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchadditives", startResult, maxRows);
		return new LinkedHashSet<Foodbatchadditive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodbatchadditive> findFoodbatchadditivesByAdditive(Integer additiveId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchadditivesByAdditive", start, max, additiveId);
		return new LinkedHashSet<Foodbatchadditive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Foodbatchadditive> findFoodbatchadditivesByFoodbatch(Integer foodbatchId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFoodbatchadditivesByFoodbatch", start, max, foodbatchId);
		return new LinkedHashSet<Foodbatchadditive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditiveByCompositeProperities(String sql, int startResult,
			int maxRows) {
		return new LinkedHashSet<Foodbatchadditive>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Foodbatchadditive> findFoodbatchadditivesByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Foodbatchadditive.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Foodbatchadditive> foodbatchadditives = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return foodbatchadditives;
	}

	@Transactional
	public Foodbatchadditive findFoodbatchadditiveByPhysicalPrimaryKey(Integer foodbatchId, Integer additiveId,
			String content) throws DataAccessException {
		return findFoodbatchadditiveByPhysicalPrimaryKey(foodbatchId, additiveId, content, -1, -1);
	}

	@Transactional
	public Foodbatchadditive findFoodbatchadditiveByPhysicalPrimaryKey(Integer foodbatchId, Integer additiveId,
			String content, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFoodbatchadditiveByPhyscialPrimaryKey", startResult, maxRows,
					foodbatchId, additiveId, content);
			return (Foodbatchadditive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Foodbatchadditive entity) {
		return true;
	}
}
